package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.util.JsonData;
import net.xdclass.request.AddressAddRequest;
import net.xdclass.service.AddressService;
import net.xdclass.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-01-26
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1/")
public class AddressController {


    @Autowired
    private AddressService addressService;

    @ApiOperation("新增收获地址")
    @PostMapping("add")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressAddRequest addressAddRequest){
        int insertFlag = addressService.add(addressAddRequest);
        if (insertFlag > 0){
            return JsonData.buildSuccess("新增收获地址成功");
        }
        return JsonData.buildError("新增收获地址失败");
    }


    @ApiOperation("根据id查找地址详情")
    @GetMapping("/find/{address_id}")
    public Object detail(
            @ApiParam(value = "地址id",required = true)
            @PathVariable("address_id") Long addressId){
        AddressVO addressVO = addressService.detail(addressId);
        if(addressVO == null){
            return JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS);
        }
    return JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("删除指定收获地址")
    @DeleteMapping("/del/{address_id}")
    public JsonData del(
            @ApiParam(value = "地址id", readOnly = true)
            @PathVariable("address_id")int addressId){

        int rows = addressService.del(addressId);
        if (rows == 1){
            return JsonData.buildSuccess("删除addressId:"+addressId+" 成功");
        }
        return JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    @ApiOperation("查询用户的全部收获地址")
    @GetMapping("/list")
    public JsonData findUserAllAdress(){

        List<AddressVO> list = addressService.listUserAllAddress();

        return JsonData.buildSuccess(list);
    }

}

